package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.GrenxinxiAdpater;
import cn.otrue.patienthealthmanager.bean.ThreadModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DialogUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.SwitchButtonListClickListener;
import cn.otrue.patienthealthmanager.view.SwitchListButton;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 健康社区
 * 
 * @author G
 * 
 */
public class HealthyCommunityActivity extends BaseActivity implements
		IXListViewListener {

	private List<ThreadModel> goods;
	private GrenxinxiAdpater postAdapter;
	private int currentPage = 1;
	private AlertDialog alertDialog;
	private TextView tv_main_info_tab_right;
	private TextView tv_main_info_tab_left;
	private String sectionName;
	private XListView lv_community_partition;
	private String sectionId;
	private int type = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_community_partition);

		initView();
	}

	private void initView() {
		goods = new ArrayList<ThreadModel>();
		lv_community_partition = (XListView) findViewById(R.id.lv_community_partition);
		lv_community_partition.setPullLoadEnable(false); // 是否开启上拉加载
		lv_community_partition.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		slb_all_doctor = (SwitchListButton) findViewById(R.id.slb_all_doctor);
		// 设置显示的文字
		String[] arr = { "全部", "精华" };
		// 设置每个textView的宽高
		// 初始化 （数据,宽,高,padding集合(默认为null),点击监听）
		slb_all_doctor.initButton(Arrays.asList(arr), 65, 35, null,
				new SwitchButtonListClickListener() {

					@Override
					public void onGetClicked(int cid) {
						Log.e("Switch", cid + "");
						goods.clear();
						type = cid + 1;
						currentPage = 0;
						if (type == 1) {
							lv_community_partition.setPullLoadEnable(false);
							goods.clear();
							gettie(currentPage);
						} else {
							lv_community_partition.setPullLoadEnable(false);
							goods.clear();
							getjinghua(currentPage);
						}
					}
				});
		// 设置初始的显示的数值
		slb_all_doctor.setClicked(0);
		getData();
		gettie(currentPage);

		initTitleBar();
		lv_community_partition
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

						int threadId = goods.get(position - 1).getThreadId();

						Intent intent = new Intent();
						intent.putExtra("threadId", threadId);
						intent.setClass(HealthyCommunityActivity.this,
								CommunityPostInfoActivity.class);
						startActivity(intent);

					}
				});

	}

	private View initDataInputDialog(DataDialogListener dataListener, int i) {
		LayoutInflater inflater = LayoutInflater
				.from(HealthyCommunityActivity.this);
		View view = inflater.inflate(R.layout.dialog_input_daaa, null);
		TextView tv_catt_dialogLeft = (TextView) view
				.findViewById(R.id.tv_catt_dialogLeft);
		tv_catt_dialogLeft.setOnClickListener(dataListener);
		TextView tv_catt_dialogRight = (TextView) view
				.findViewById(R.id.tv_catt_dialogRight);
		tv_catt_dialogRight.setOnClickListener(dataListener);
		return view;

	}

	public class DataDialogListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.tv_catt_dialogLeft:// 取消
				alertDialog.dismiss();
				break;
			case R.id.tv_catt_dialogRight:// 确定

				Intent personinfoIntent = new Intent(
						HealthyCommunityActivity.this, LoginActivity.class);
				startActivity(personinfoIntent);
				alertDialog.dismiss();
			}
		}

	}

	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(sectionName);
		TextView tv_title_other = (TextView) findViewById(R.id.tv_title_ot);

		tv_title_other.setVisibility(View.VISIBLE);
		tv_title_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MyConst.getACCOUNT().equals("")) {
					View dialogView = initDataInputDialog(
							new DataDialogListener(), 0);
					if (null != dialogView)
						alertDialog = DialogUtil
								.showDialog(HealthyCommunityActivity.this,
										dialogView, true);
				} else {

					Intent intent = new Intent();

					intent.putExtra("sectionId", sectionId);
					intent.setClass(HealthyCommunityActivity.this,
							CommunityPostWriteActivity.class);

					startActivity(intent);
				}
			}
		});

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private void getData() {

		sectionId = getIntent().getStringExtra("sectionId");
		Log.e("FFFFF", sectionId + "");
		sectionName = getIntent().getStringExtra("sectionName");
	}

	private Handler faHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String tuu = data.getString(HttpCons.DATA);
				Log.e("liyu", tuu);
				List<ThreadModel> iiii = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<ThreadModel>>() {
						}.getType());
				goods.addAll(iiii);
				if (goods.size() > 5) {
					lv_community_partition.setPullLoadEnable(true);
				}
				postAdapter = new GrenxinxiAdpater(goods,
						HealthyCommunityActivity.this);
				lv_community_partition.setAdapter(postAdapter);
				postAdapter.notifyDataSetChanged();

				Toast.makeText(HealthyCommunityActivity.this, "操作成功", 0).show();
			} else {
				onLoad();
				Toast.makeText(HealthyCommunityActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(HealthyCommunityActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private SwitchListButton slb_all_doctor;

	private void gettie(int i) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("sectionId", sectionId);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setCurrentPage(i + "");
		parameter.setPagesize("20");
		parameter.setData(map);
		new HttpServer().post(HttpCons.query_Thread_List, parameter, faHandler);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		lv_community_partition.setPullLoadEnable(false);
		if (type == 1) {
			goods.clear();
			gettie(currentPage);

		} else {
			goods.clear();
			getjinghua(currentPage);
		}

		super.onResume();

	}

	private void onLoad() {
		lv_community_partition.stopRefresh();
		lv_community_partition.stopLoadMore();
		lv_community_partition.setRefreshTime("刚刚");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		// case R.id.tv_main_info_tab_left:
		// type = 1;
		// goods.clear();
		// gettie(currentPage);
		// tv_main_info_tab_left
		// .setBackgroundResource(R.color.switch_butto_open);
		// tv_main_info_tab_right
		// .setBackgroundResource(R.color.switch_butto_off);
		// break;
		// case R.id.tv_main_info_tab_right:
		// tv_main_info_tab_right
		// .setBackgroundResource(R.color.switch_butto_open);
		// tv_main_info_tab_left
		//
		// .setBackgroundResource(R.color.switch_butto_off);
		// goods.clear();
		// type = 2;
		// getjinghua(currentPage);
		// break;

		default:
			break;
		}

	}

	private void getjinghua(int i) {
		postAdapter.notifyDataSetChanged();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("sectionId", sectionId);
		map.put("isExcel", 1);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setCurrentPage(i + "");
		parameter.setPagesize("20");
		parameter.setPassword(" ");
		parameter.setData(map);
		new HttpServer().post(HttpCons.Essential_Posts, parameter, faHandler);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		currentPage = 1;
		goods.clear();

		if (type == 1) {
			gettie(currentPage);
		} else {
			getjinghua(currentPage);
		}

	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		currentPage++;
		if (type == 1) {
			gettie(currentPage);
		} else {
			getjinghua(currentPage);
		}

	}

}
